<?php namespace App\Utils;
/**
 * Utility class for managing the files used in the system.
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tinycrawler.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2012  Tinycrawler Technologies USA Inc. (http://www.tinycrawler.com)
 * @license    New BSD License
 * @version    2012-10-27 21:25:49Z chengxu $
 */

/**
 * CsvTool
 *
 * Properties:
 * <ul>
 *
 * <li>Method: readCSVFile</li>
 * <li>Method: writeCSVFile</li>
 * </ul>
 */

class CsvTool {
    /**
     * Reads the CSV/TSV files specified by $csvFile and output
     * the formatted key/value rows into the returned array.
     * To read the TSV file, specify the parameter $delimiter to
     * the delimiter character used in TSV file. (Usually, \t)
     * Example usage:
     * <code>readCSVFile($csvFile, '\t')</code>
     *
     * @param string $csvFile the input csv/tsv file to load
     * @param string $delimiter the file delimiter in the input file
     * @return array (header => the header of the csv/tsv file,
     *                data => the content of the file.)
     */
    function readCSVFile($csvFile, $delimiter = ",") {
        $rows = array();
        $headers = array();
        $row = 1;
        if (($handle = fopen($csvFile, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
                if ($row == 1) {
                    $index = 0;
                    foreach($data as $item) {
                        $item = trim($item);
                        $headers[$item] = $index;
                        $index++;
                    }
                } else {
                    $num = count($data);
                    array_push($rows, $data);
                }
                $row++;
            }
            fclose($handle);
        }
        $ret = array ('headers' => $headers, 'data' => $rows);
        return $ret;
    }

    /**
     * Write to a specified CSV/TSV files using the parameters
     * (headers, content)
     * To write the TSV file, specify the parameter $delimiter to
     * the delimiter character used in TSV file. (Usually, \t)
     * Example usage:
     * <code>$header=array('product','quantity');</code>
     * <code>$content=array(1 => array('iPad','100'));</code>
     * <code>writeCSVFile($csvFile, $header, $content, '\t')</code>
     *
     * @param string $file_name the output csv/tsv file to write
     * @param array $header the header rows in output csv/tsv file
     * @param array^2 $content a two dimension array, keyed by rows and then each cells in columns.
     * @param string $delimiter the file delimiter in the input file
     * @return boolean if the file is created or not.
     */
    function writeCSVFile($file_name, $output_header, $output, $delimiter = ",") {
        $fp = fopen($file_name, 'w');
        if ($fp == false) {
            return false;
        }
        fputcsv($fp, $output_header, $delimiter);
        foreach ($output as $fields) {
            fputcsv($fp, $fields, $delimiter);
        }
        fclose($fp);
        return true;
    }

    /**
     * test() functions is used to make some testing on the classes and functions in this
     * file. The common usage of 'test()' function is used to independently test the
     * functionality the classes/functions in this file working properly.
     */
    function test() {
    }
}
?>